package com.seven.springboot.util;

import java.util.ArrayList;
import java.util.List;

public class ListUtils {

	/**
	 * 切割list,常用在分段数据中，分页中
	 * 
	 * @param list 要切割的list
	 * @param fromIndex 开始 的index
	 * @param count 切割的数量
	 * @return
	 */
	public static <T> List<T> subList(List<T> list, int fromIndex, int count) {
		if(list == null || list.isEmpty()) {
			return null;
		}
		int maxSize = list.size();
		if(fromIndex >= maxSize) {
			return null;
		}
		
		if(fromIndex < 0) {
			fromIndex = 0;
		}
		//count必须不能小于0
		if(count < 0) {
			count = 0;
		}
		int toIndex = Math.min(fromIndex + count, maxSize);
		return list.subList(fromIndex, toIndex);
	}
	
	public static <T> Boolean  isEmpty(List<T> list) {
		if (list != null && list.size()>0) {
			return false;
		}
		return true;
	}
	/**
	 * 将list按blockSize大小等分，最后多余的单独一份
	 * @author daidalei
	 * @date 2017-3-13 上午10:45:42
	 */
	public static <T> List<List<T>> subList(List<T> list, int blockSize) {  
        List<List<T>> lists = new ArrayList<List<T>>();  
        if (list != null && blockSize > 0) {  
            int listSize = list.size();  
            if(listSize<=blockSize){  
                lists.add(list);  
                return lists;  
            }  
            int batchSize = listSize / blockSize;  
            int remain = listSize % blockSize;  
            for (int i = 0; i < batchSize; i++) {  
                int fromIndex = i * blockSize;  
                int toIndex = fromIndex + blockSize;  
                lists.add(list.subList(fromIndex, toIndex));  
            }  
            if(remain>0){  
                lists.add(list.subList(listSize-remain, listSize));  
            }  
        }  
        return lists;  
    }
}
